﻿using System;
using System.IO;
using System.Xml.Serialization;

namespace _035_自定义数组类型成员的XML元素
{
    class Program
    {
        static void Main(string[] args)
        {

            声明类之一 ss1 = new 声明类之一();
            ss1.Items = new string[] { "item1", "item2", "item3" };
            ss1.ItemCount = ss1.Items.Length;
            Console.WriteLine("默认序列化方案:");
            序列方法(ss1);


            声明类之二 ss2 = new 声明类之二();
            ss2.Items = new string[]{"item1", "item2", "item3"};
            ss2.ItemCount = ss2.Items.Length;
            Console.WriteLine("自定义序列化方案：");
            序列方法(ss2);



            Console.Read();


        }



        static void 序列方法<T>(T obj)
        {
            using (MemoryStream 操作 = new MemoryStream())
            {
                XmlSerializer 延展 = new XmlSerializer(typeof(T));
                延展.Serialize(操作, obj);

                操作.Position = 0L;
                using (StreamReader 顺之 = new StreamReader(操作))
                {
                    string 文件 = 顺之.ReadToEnd();
                    Console.Write(文件 + "\n\n");
                }
            }
        }

    }


    public class 声明类之一
    {
        public int ItemCount;
        public string[] Items;
    }

    public class 声明类之二
    {
        public int ItemCount;
        [XmlArray("item_list")]
        [XmlArrayItem("sub_item")]
        public string[] Items;
    }
}
